Appendix A — Assignment 1 (Control flow)

Instructions

  1. You may talk to a friend, discuss the questions and potential directions for solving them. However, you need to write your own solutions and code separately, and not as a group activity.

  2. Do not write your name on the assignment.

  3. Write your code in the Code cells of the Jupyter notebook. Ensure that the solution is written neatly enough to understand and grade.

  4. Use Quarto to print the .ipynb file as HTML. You will need to open the command prompt, navigate to the directory containing the file, and use the command: quarto render filename.ipynb --to html. Submit the HTML file.

  5. There are 5 points for clealiness and organization. The code should be commented and clearly written with intuitive variable names. For example, use variable names such as number_input, factor, hours, instead of a,b,xyz, etc.

  6. The assignment is worth 100 points, and is due on 5th October 2022 at 11:59 pm.

B 1

B.1 1(a)

You look at the clock and it is exactly 2pm. You set an alarm to go off in 510 hours. At what time does the alarm go off? If the answer is say, 4 pm, then your code should print - “The alarm goes off at 4 pm”.

(5 points)

B.2 1(b)

Write a program to solve the general version of the above problem. Ask the user for - (1) the time now (in hours), and (2) the number of hours for the alarm to go off. Your program should output the time at which the alarm goes off. Both the user inputs must be in {0, 1, 2…, 22, 23}. If the answer is, say 14:00 hours, then your program should print - “The alarm goes off at 14:00 hours.

Show the output of your program when the user inputs 7 as the current time, and 95 as the number of hours for the alarm to go off.

(10 points)

C 2

C.1 2(a)

Write a program that checks if a positive integer is prime or not. Show the output when the program is used to check if 89 is prime or not.

(5 points)

C.2 2(b)

Prompt the user to input a positive integer. Write a program that prints the factors of the positive integer input by the user. Show the output of the program if the user inputs 190.

(10 points)

C.3 2(c)

Prompt the user to input a positive integer. Update the program in 2(b) to print the prime factors of the positive integer input by the user. Show the output of the program if the user inputs 190.

(15 points)

C.4 2(d)

Update the program in 2(c), so that it prints “Incorrect input, please enter positive integer” if the user does not enter a positive integer, and then prompts the user to input a positive integer. The program should continue to prompt the user to enter a positive integer until the user successfully enters a positive integer. Show the output of the program if the user enters “seventy” in the first attempt, “#70” in the second attempt, and 70 in the third attempt.

(30 points)

C.5 3

Prompt the user to input an english sentence. Write a program that counts and prints the number of words in the sentence input by the user. The program should continue to run until the user inputs the sentence - “end program”

(20 points)